<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 发票 服务控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\service\FinanceService;
use think\Request;

class InvoiceController extends VerifyController
{
    protected $financeService;

    /**
     * InvoiceController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->financeService = new FinanceService();
    }

    /**
     * 上传发票
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function uploadInvoice(Request $request)
    {
        $param = $request->param();
        $resData = $this->financeService->uploadInvoice($param['data'], auto_fill_tenant_id());
        return json(success_response('', $resData));
    }

    /**
     * 确认发票
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     * @throws \think\Exception
     */
    public function ensureInvoice(Request $request)
    {
        $param = $request->param();
        $resData = $this->financeService->ensureInvoice($param['param'], $request->getUserId());
        return json(success_response('', $resData));
    }

    /**
     * 获得开票申请列表
     * @return \think\Response
     * @throws \think\Exception
     * @throws \Exception
     */
    public function getCustomerInvoiceApplyList(Request $request)
    {
        $param = $request->param();
        $resData = $this->financeService->getCustomerInvoiceApplyList($param['param'], $request->getTenantId());
        return json(success_response('', $resData));
    }

    /**
     * 创建发票申请
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     * @throws \think\Exception
     */
    public function createCustomerInvoiceApply(Request $request)
    {
        $param = $request->param();
        $resData = $this->financeService->createCustomerInvoiceApply($param['data'], $request->getUserId(), $request->getTenantId());
        return json(success_response('', $resData));
    }

    /**
     * 上传发票给发票申请
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     * @throws \think\Exception
     */
    public function uploadInvoiceCustomerInvoiceApply(Request $request)
    {
        $param = $request->param();
        $resData = $this->financeService->uploadInvoiceCustomerInvoiceApply($param['data']);
        return json(success_response('', $resData));
    }
}
